function security_tool() {

};

// 获取随机数的方法，n为获取随机数的范围
security_tool.prototype.getRandom = function(n) {
	return Math.floor(Math.random() * n + 1);
};

// 获取Xb
security_tool.prototype.encrypt = function() {
	var Xb = Math.floor(Math.random() * 31 + 1);
	var p = $.cookie("p");
	var g = $.cookie("g");
	var Ya = $.cookie("Ya");
	// 计算Yb
	var Yb = Math.pow(g, Xb) % p;
	var password = $('.password').val();
	// 对密码进行加密
	var bytes = password.split("");
	var k = Math.pow(Ya, Xb) % p;
	var cookietime = new Date();
	cookietime.setTime(cookietime.getTime() + (5 * 60 * 1000));// coockie保存五分钟
	for (var i = 0; i < bytes.length; i++) {
		bytes[i] = parseInt(bytes[i].charCodeAt()) + parseInt(k) + "";
	}
	$('.password').val(bytes);
	$.cookie("Yb", Yb, {
		path : "/",
		expires : cookietime
	});
};

jQuery(document)
		.ready(
				function() {
					// 处理后台传过来的提示信息
					$(".login-msg").css("display", "none");
					var login_msg = $.cookie("login-msg");
					if (login_msg != null && login_msg.trim() != "") {
						$(".login-msg").text(login_msg);
						$(".login-msg").css("display", "inline");
					}
					$.cookie("login-msg", null, {
						path : "/"
					});
					// 提交后要做的事情
					$('.page-container form')
							.submit(
									function() {
										var username = $(this)
												.find('.username').val();
										var password = $(this)
												.find('.password').val();
										if (username == '') {
											$(this).find('.error').fadeOut(
													'fast',
													function() {
														$(this).css('top',
																'27px');
													});
											$(this).find('.error').fadeIn(
													'fast',
													function() {
														$(this).parent().find(
																'.username')
																.focus();
													});
											return false;
										}
										if (password == '') {
											$(this).find('.error').fadeOut(
													'fast',
													function() {
														$(this).css('top',
																'96px');
													});
											$(this).find('.error').fadeIn(
													'fast',
													function() {
														$(this).parent().find(
																'.password')
																.focus();
													});
											return false;
										}

										/*
										 * if (password.trim() != '') { var p =
										 * $.cookie("p"); var g = $.cookie("g");
										 * var Ya = $.cookie("Ya"); if (p ==
										 * null || g == null || Ya == null)
										 * $.ajax({ url:"updateKey",
										 * type:"post", success:function(){ p =
										 * $.cookie("p"); g = $.cookie("g"); Ya =
										 * $.cookie("Ya"); } }); var Xb =
										 * getRandom(31); var Yb = Math.pow(g,
										 * Xb)%p; var bytes =
										 * password.split(""); var k =
										 * Math.pow(Ya, Xb)%p; var cookietime =
										 * new Date();
										 * cookietime.setTime(cookietime.getTime() +
										 * (5 * 60 * 1000));//coockie保存五分钟
										 * for(var i=0; i<bytes.length;i++) {
										 * bytes[i] =
										 * parseInt(bytes[i].charCodeAt())+parseInt(k)+""; }
										 * $(this).find('.password').val(bytes);
										 * $.cookie("Yb", Yb,
										 * {path:"/",expires:cookietime}); } });
										 */

										$(
												'.page-container form .username, .page-container form .password')
												.keyup(
														function() {
															$(this)
																	.parent()
																	.find(
																			'.error')
																	.fadeOut(
																			'fast');
														});
										if (password.trim() != ''
												&& username.trim() != '') {
											//获取key
											var firstKey = new security_tool().getRandom(9)+"";
											var secondKey = new security_tool().getRandom(9)+"";
											var thirdKey = new security_tool().getRandom(9)+"";
											$('.firstKey').val(firstKey);
											$('.secondKey').val(secondKey);
											$('.thirdKey').val(thirdKey);
											password = strEnc(password,firstKey,secondKey,thirdKey);
											$('.password').val(password);
										}
									});
				});
